package com.zzc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzc.common.JsonResult;
import com.zzc.common.TableResult;
import com.zzc.mapper.AppInfoMapper;
import com.zzc.mapper.RoleMapper;
import com.zzc.mapper.UserTokenMapper;
import com.zzc.pojo.Role;
import com.zzc.pojo.UserToken;
import com.zzc.service.RoleService;
import com.zzc.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhouzhicheng
 * @version 1.0  2020/12/29
 */
@Service
public class TokenServiceImpl implements TokenService {
    @Autowired
    private UserTokenMapper userTokenMapper;
    @Autowired
    private AppInfoMapper appInfoMapper;

    @Override
    public TableResult getTokenList(String accessToken,Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        PageInfo<UserToken> pageInfo = new PageInfo<>(userTokenMapper.getTokenList(accessToken));
        return new TableResult(0,null,Long.valueOf(pageInfo.getTotal()).intValue(), pageInfo.getList());
    }

    @Override
    public JsonResult deleteTokenById(Integer[] ids) {
        if (ids==null){
            return JsonResult.getResultFailed("参数错误！");
        }
        for (Integer id : ids) {
            userTokenMapper.deleteByPrimaryKey(id);
        }
        return JsonResult.getResultSuccess();
    }

    @Override
    public JsonResult updateByPrimaryKeySelective(UserToken token) {
        //给属性appId赋值
        token.setAppId(appInfoMapper.getAppInfoByCusId(token.getCusId()).getId());
        System.out.println(token);
        return userTokenMapper.updateByPrimaryKeySelective(token)>0
                ? JsonResult.getResultSuccess()
                : JsonResult.getResultFailed("修改失败！");
    }
}
